package com.guaoran.controller;

import com.github.pagehelper.PageInfo;
import com.guaoran.jpa.entity.VpgMovie;
import com.guaoran.jpa.entity.VpgPackage;
import com.guaoran.mybatis.bean.Demo;
import com.guaoran.service.DemoService;
import com.guaoran.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author : 孤傲然
 * @Description :
 * @date :2018/1/25 13:37
 */
@Slf4j
@Controller
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private DemoService demoService;

    /**
     * 使用jpa分页
     * @param title
     * @param actors
     * @param director
     * @param pageSize
     * @param pageNum
     * @param modelMap
     * @return
     */
    @ApiIgnore
    @GetMapping("/queryAllMovie")
    public String queryAllMovie(String title, String actors, String director,Integer pageSize,Integer pageNum,ModelMap modelMap){
        log.info("title={},actors={},director={}",title,actors,director);
        if(pageSize==null){
            pageSize = 2;
        }
        if(pageNum==null){
            pageNum = 0;
        }
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(pageNum,pageSize);
        Page<VpgMovie> vpgMovies = demoService.findDistinctByTitleContainingOrActorsContainingOrDirectorContaining(title, actors, director, pageable);
        modelMap.addAttribute("vpgMovies",vpgMovies);
        return "/freemarker/demo";
    }
    @GetMapping("/queryAllPackage")
    public ModelAndView queryAllPackage(@RequestParam(value = "providerId") String providerId){
        List<VpgPackage> vpgPackageList = demoService.findVpgPackagesByProviderId(providerId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("vpgPackageList",vpgPackageList);
        modelAndView.setViewName("/freemarker/demo2");
        return modelAndView;
    }

    /**
     * 使用jpa分页
     * @param providerId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/queryPackage")
    public ModelAndView queryPackage(@RequestParam(value = "providerId") String providerId,Integer pageSize,Integer pageNum){
        if(pageSize==null){
            pageSize = 10;
        }
        if(pageNum==null){
            pageNum = 0;
        }

        Pageable pageable = new PageRequest(pageNum,pageSize);
        Page<VpgPackage> vpgPackageList = demoService.findAllByProviderIdOrderByAssetName(providerId,pageable);
        log.info("vpgPackageList={}",vpgPackageList.toString());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("vpgPackageList",vpgPackageList);
        modelAndView.setViewName("/freemarker/demo3");
        modelAndView.addObject("vpgMovies",vpgPackageList);
        modelAndView.addObject("totalNum",vpgPackageList.getTotalElements());
        modelAndView.addObject("totalPages",vpgPackageList.getTotalPages());
        modelAndView.addObject("number",vpgPackageList.getNumber());
        modelAndView.addObject("numberElement",vpgPackageList.getNumberOfElements());
        return modelAndView;
    }

    /**
     * 查询分页并排序
     * @param pageSize
     * @param pageNum
     * @param orderBy
     * @return
     */
    @GetMapping("/queryDemoByPageOrderBy")
    public ModelAndView queryDemoByPageOrderBy(Integer pageSize,Integer pageNum,String orderBy){
        if(pageNum==null){
            pageNum = 0;
        }
        if(pageSize==null){
            pageSize = 4;
        }
        ModelAndView modelAndView = new ModelAndView();
        if(CommonUtils.isEmpty(orderBy)){
            orderBy = "id";
        }
        PageInfo<Demo> demoPageInfo = demoService.queryDemoByHelperPageOrderBy(pageNum, pageSize,orderBy);
        modelAndView.addObject("pageInfo",demoPageInfo);
        modelAndView.setViewName("/freemarker/demoPageHelperOrderBy");
        return modelAndView;
    }
    /**
     * 查询分页
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/queryDemoByPage")
    public ModelAndView queryDemoByPage(Integer pageSize,Integer pageNum){
        if(pageNum==null){
            pageNum = 0;
        }
        if(pageSize==null){
            pageSize = 4;
        }
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Demo> demoPageInfo = demoService.queryDemoByHelperPage(pageNum, pageSize);
        modelAndView.addObject("pageInfo",demoPageInfo);
        modelAndView.setViewName("/freemarker/demoPageHelper");
        return modelAndView;
    }
}
